package com.jht.bletool2java.activity.homepage.fragment.bledevice;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.util.Log;


import com.jht.bletool2java.BLEApplication;
import com.jht.bletool2java.R;
import com.jht.bletool2java.activity.homepage.fragment.bledevice.bean.BLEDevice;

public class BleDevicesFragmentPresenter implements IBLEDevice.Presenter{
    private static final String TAG = "BTFrag_Presenter";
    private IBLEDevice.UI bleDevicesFragment;

    private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, final byte[] scanRecord) {
            if ("".equals(device.getName()) || device.getName() == null) {
                return;
            }
            BLEDevice bleDevice = new BLEDevice(device,rssi,scanRecord);
            bleDevicesFragment.addBleDevice(bleDevice);
        }

    };



    public BleDevicesFragmentPresenter(IBLEDevice.UI bleDevicesFragment) {
        this.bleDevicesFragment = bleDevicesFragment;
    }

    @Override
    public void startScanBle(BluetoothAdapter bluetoothAdapter) {
        if(bluetoothAdapter == null){
            bleDevicesFragment.showToast(R.string.bt_adapter_null);
        }else {
            bleDevicesFragment.initScanUI();
            bluetoothAdapter.startLeScan(leScanCallback);
            Log.d(TAG, "startScanBle: delay time = " + BLEApplication.getBLEConfiguration().getScanTime() + " 秒");
            bleDevicesFragment.stopBtScan(BLEApplication.getBLEConfiguration().getScanTime() * 1000);
        }
    }

    @Override
    public void stopScanBle(BluetoothAdapter bluetoothAdapter){
        bluetoothAdapter.stopLeScan(leScanCallback);
    }

}
